package com.jindy.module.fileUploadRecord.service.impl;

import java.util.List;

import com.jindy.common.constant.Constants;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.utils.DateUtils;
import com.jindy.file.domain.FileUploadEntity;
import com.jindy.file.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.fileUploadRecord.mapper.JinFileUploadRecordMapper;
import com.jindy.module.fileUploadRecord.domain.JinFileUploadRecord;
import com.jindy.module.fileUploadRecord.service.IJinFileUploadRecordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传记录Service业务层处理
 * 
 * @author jindy
 * @date 2023-04-26
 */
@Service
public class JinFileUploadRecordServiceImpl implements IJinFileUploadRecordService 
{
    @Autowired
    private JinFileUploadRecordMapper jinFileUploadRecordMapper;

    @Autowired
    private FileUtils fileUtils;

    /**
     * 查询文件上传记录
     * 
     * @param id 文件上传记录主键
     * @return 文件上传记录
     */
    @Override
    public JinFileUploadRecord selectJinFileUploadRecordById(Long id)
    {
        return jinFileUploadRecordMapper.selectJinFileUploadRecordById(id);
    }

    /**
     * 查询文件上传记录列表
     * 
     * @param jinFileUploadRecord 文件上传记录
     * @return 文件上传记录
     */
    @Override
    public List<JinFileUploadRecord> selectJinFileUploadRecordList(JinFileUploadRecord jinFileUploadRecord)
    {
        return jinFileUploadRecordMapper.selectJinFileUploadRecordList(jinFileUploadRecord);
    }

    /**
     * 新增文件上传记录
     * 
     * @param jinFileUploadRecord 文件上传记录
     * @return 结果
     */
    @Override
    public int insertJinFileUploadRecord(JinFileUploadRecord jinFileUploadRecord)
    {
        jinFileUploadRecord.setCreateTime(DateUtils.getNowDate());
        return jinFileUploadRecordMapper.insertJinFileUploadRecord(jinFileUploadRecord);
    }

    /**
     * 修改文件上传记录
     * 
     * @param jinFileUploadRecord 文件上传记录
     * @return 结果
     */
    @Override
    public int updateJinFileUploadRecord(JinFileUploadRecord jinFileUploadRecord)
    {
        jinFileUploadRecord.setUpdateTime(DateUtils.getNowDate());
        return jinFileUploadRecordMapper.updateJinFileUploadRecord(jinFileUploadRecord);
    }

    /**
     * 批量删除文件上传记录
     * 
     * @param ids 需要删除的文件上传记录主键
     * @return 结果
     */
    @Override
    public int deleteJinFileUploadRecordByIds(Long[] ids)
    {
        return jinFileUploadRecordMapper.deleteJinFileUploadRecordByIds(ids);
    }

    /**
     * 删除文件上传记录信息
     * 
     * @param id 文件上传记录主键
     * @return 结果
     */
    @Override
    public int deleteJinFileUploadRecordById(Long id)
    {
        return jinFileUploadRecordMapper.deleteJinFileUploadRecordById(id);
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult uploadFile(MultipartFile file) {
        FileUploadEntity upload = fileUtils.upload(file);
        JinFileUploadRecord jinFileUploadRecord = new JinFileUploadRecord();
        jinFileUploadRecord.setFilePath(upload.getFilePath());
        jinFileUploadRecord.setServerFileName(upload.getServerFileName());
        jinFileUploadRecord.setSize(upload.getSize());
        jinFileUploadRecord.setVisitUrl(upload.getVisitUrl());
        jinFileUploadRecord.setState(Constants.DATA_VALID_STATE);
        jinFileUploadRecordMapper.insertJinFileUploadRecord(jinFileUploadRecord);
        return AjaxResult.success(upload);
    }
}
